# MongoDB 自动化安装脚本（Windows）
# 使用管理员权限运行

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  MongoDB 本地安装助手" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Write-Host "❌ 请以管理员身份运行此脚本！" -ForegroundColor Red
    Write-Host ""
    Write-Host "操作步骤：" -ForegroundColor Yellow
    Write-Host "1. 右键点击 PowerShell" -ForegroundColor Yellow
    Write-Host "2. 选择'以管理员身份运行'" -ForegroundColor Yellow
    Write-Host "3. 重新运行此脚本" -ForegroundColor Yellow
    Write-Host ""
    pause
    exit
}

Write-Host "✅ 管理员权限确认" -ForegroundColor Green
Write-Host ""

# 步骤 1：检查是否已安装 MongoDB
Write-Host "📋 步骤 1: 检查 MongoDB 安装状态" -ForegroundColor Cyan
Write-Host "检查中..." -ForegroundColor Yellow

$mongoService = Get-Service -Name MongoDB -ErrorAction SilentlyContinue

if ($mongoService) {
    Write-Host "✅ MongoDB 已安装！" -ForegroundColor Green
    Write-Host "   服务名称: $($mongoService.Name)" -ForegroundColor Gray
    Write-Host "   服务状态: $($mongoService.Status)" -ForegroundColor Gray
    Write-Host ""
    
    if ($mongoService.Status -eq "Running") {
        Write-Host "✅ MongoDB 服务正在运行" -ForegroundColor Green
    } else {
        Write-Host "⚠️  MongoDB 服务未运行，正在启动..." -ForegroundColor Yellow
        Start-Service MongoDB
        Write-Host "✅ MongoDB 服务已启动" -ForegroundColor Green
    }
    
    Write-Host ""
    Write-Host "🎉 MongoDB 已准备就绪！" -ForegroundColor Green
    Write-Host ""
    Write-Host "下一步操作：" -ForegroundColor Cyan
    Write-Host "1. 测试连接: npx ts-node src/tests/simple-test.ts" -ForegroundColor White
    Write-Host "2. 启动项目: npm run dev" -ForegroundColor White
    Write-Host ""
    pause
    exit
}

Write-Host "ℹ️  未检测到 MongoDB 安装" -ForegroundColor Yellow
Write-Host ""

# 步骤 2：打开下载页面
Write-Host "📋 步骤 2: 打开 MongoDB 下载页面" -ForegroundColor Cyan
Write-Host ""
Write-Host "即将打开浏览器..." -ForegroundColor Yellow

Start-Process "https://www.mongodb.com/try/download/community"
Start-Sleep -Seconds 2

Write-Host ""
Write-Host "✅ 浏览器已打开" -ForegroundColor Green
Write-Host ""

# 步骤 3：下载和安装说明
Write-Host "📋 步骤 3: 下载并安装 MongoDB" -ForegroundColor Cyan
Write-Host ""
Write-Host "请在浏览器中完成以下操作：" -ForegroundColor Yellow
Write-Host ""
Write-Host "1️⃣  选择版本：" -ForegroundColor White
Write-Host "   - Version: 7.0.x 或 6.0.x" -ForegroundColor Gray
Write-Host "   - Platform: Windows" -ForegroundColor Gray
Write-Host "   - Package: msi" -ForegroundColor Gray
Write-Host ""
Write-Host "2️⃣  点击 Download 下载" -ForegroundColor White
Write-Host ""
Write-Host "3️⃣  下载完成后，双击 .msi 文件安装" -ForegroundColor White
Write-Host ""
Write-Host "4️⃣  安装过程中的重要设置：" -ForegroundColor White
Write-Host "   ✅ 选择 'Complete' 完整安装" -ForegroundColor Gray
Write-Host "   ✅ 勾选 'Install MongoDB as a Service'" -ForegroundColor Gray
Write-Host "   ✅ Service Name 保持默认 'MongoDB'" -ForegroundColor Gray
Write-Host "   ✅ Run as 选择 'Network Service user'" -ForegroundColor Gray
Write-Host "   ✅ 勾选 'Install MongoDB Compass'（可选）" -ForegroundColor Gray
Write-Host ""
Write-Host "按任意键继续..." -ForegroundColor Yellow
pause

# 步骤 4：等待安装完成
Write-Host ""
Write-Host "📋 步骤 4: 验证安装" -ForegroundColor Cyan
Write-Host ""
Write-Host "请等待 MongoDB 安装完成..." -ForegroundColor Yellow
Write-Host "安装通常需要 3-5 分钟" -ForegroundColor Gray
Write-Host ""
Write-Host "安装完成后，按任意键继续验证..." -ForegroundColor Yellow
pause

# 步骤 5：验证安装
Write-Host ""
Write-Host "📋 步骤 5: 检查 MongoDB 服务" -ForegroundColor Cyan
Write-Host ""

$retryCount = 0
$maxRetries = 5

while ($retryCount -lt $maxRetries) {
    $mongoService = Get-Service -Name MongoDB -ErrorAction SilentlyContinue
    
    if ($mongoService) {
        Write-Host "✅ MongoDB 服务已安装！" -ForegroundColor Green
        Write-Host "   状态: $($mongoService.Status)" -ForegroundColor Gray
        
        if ($mongoService.Status -ne "Running") {
            Write-Host "   正在启动服务..." -ForegroundColor Yellow
            Start-Service MongoDB
            Start-Sleep -Seconds 2
            Write-Host "✅ 服务已启动" -ForegroundColor Green
        }
        
        break
    }
    
    $retryCount++
    Write-Host "⏳ 等待服务注册... ($retryCount/$maxRetries)" -ForegroundColor Yellow
    Start-Sleep -Seconds 5
}

if (-not $mongoService) {
    Write-Host ""
    Write-Host "❌ 未检测到 MongoDB 服务" -ForegroundColor Red
    Write-Host ""
    Write-Host "可能的原因：" -ForegroundColor Yellow
    Write-Host "1. 安装未完成" -ForegroundColor Gray
    Write-Host "2. 安装时未勾选 'Install as Service'" -ForegroundColor Gray
    Write-Host "3. 需要重启计算机" -ForegroundColor Gray
    Write-Host ""
    Write-Host "解决方案：" -ForegroundColor Yellow
    Write-Host "1. 检查安装是否完成" -ForegroundColor Gray
    Write-Host "2. 重新运行此脚本" -ForegroundColor Gray
    Write-Host "3. 查看详细文档: MONGODB_LOCAL_INSTALL.md" -ForegroundColor Gray
    Write-Host ""
    pause
    exit
}

# 步骤 6：测试连接
Write-Host ""
Write-Host "📋 步骤 6: 测试数据库连接" -ForegroundColor Cyan
Write-Host ""

# 检查 mongosh 是否可用
$mongoshPath = Get-Command mongosh -ErrorAction SilentlyContinue

if ($mongoshPath) {
    Write-Host "✅ mongosh 命令可用" -ForegroundColor Green
} else {
    Write-Host "⚠️  mongosh 命令不可用" -ForegroundColor Yellow
    Write-Host "   正在添加到环境变量..." -ForegroundColor Yellow
    
    $mongoPath = "C:\Program Files\MongoDB\Server\7.0\bin"
    if (-not (Test-Path $mongoPath)) {
        $mongoPath = "C:\Program Files\MongoDB\Server\6.0\bin"
    }
    
    if (Test-Path $mongoPath) {
        $env:Path += ";$mongoPath"
        Write-Host "✅ 已添加到当前会话" -ForegroundColor Green
    }
}

Write-Host ""
Write-Host "📋 步骤 7: 配置完成" -ForegroundColor Cyan
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "  🎉 MongoDB 安装成功！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "📊 服务信息：" -ForegroundColor Cyan
Get-Service MongoDB | Format-Table -Property Status, Name, DisplayName
Write-Host ""
Write-Host "🚀 下一步操作：" -ForegroundColor Cyan
Write-Host ""
Write-Host "1. 测试数据库连接：" -ForegroundColor White
Write-Host "   cd D:\LYHProject\LYH5\backend" -ForegroundColor Gray
Write-Host "   npx ts-node src/tests/simple-test.ts" -ForegroundColor Gray
Write-Host ""
Write-Host "2. 启动项目：" -ForegroundColor White
Write-Host "   npm run dev" -ForegroundColor Gray
Write-Host ""
Write-Host "3. 访问 API：" -ForegroundColor White
Write-Host "   http://localhost:3000/api/books" -ForegroundColor Gray
Write-Host ""
Write-Host "📚 查看详细文档：" -ForegroundColor Cyan
Write-Host "   MONGODB_LOCAL_INSTALL.md" -ForegroundColor Gray
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
pause
